/**
 * 
 */
package com.taobao.client.pages;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.taobao.client.HeadPanel;
import com.taobao.client.RPC;
import com.taobao.client.sdk.RefundHandleRequest;
import com.taobao.client.util.PopBox;

/**
 * Apply the singleton design pattern.
 * 
 * @author hujingfei.pt
 *
 */
public class RefundTermUpdate extends Composite implements ClickHandler{

	private static RefundTermUpdate refundTermUpdate;		
	private final static String RESULT_FLAG = "ResultCode";		
	private Frame f = null;	
	private AsyncCallback<String> inputCallBack = new AsyncCallback<String>() {
		@Override
		public void onFailure(Throwable arg0) {
			PopBox.error("Construct URL Error", arg0.getMessage());
		}

		@Override
		public void onSuccess(String url) {
//			f.setUrl(url);
//			f.setSize(Data.width, Data.height);
//			Window.open(url, null, null);
			PopBox.openWindow(url);
		}
	};
	// A vertical panel which contains various labels and textareas
	private VerticalPanel container = new VerticalPanel();
	private Label aucRemarkLabel = new Label("Auction Remark");
	private TextArea aucRemarkTArea = new TextArea();
	private Label ordRemarkLabel = new Label("Order Remark");
	private TextArea ordRemarkTArea = new TextArea();
	private Button okButton = new Button("Update Refund Term");
	
	public RefundHandleRequest input;
	public final static String HISTORY_ITEM = "RefundTermUpdatePage";	
	
	/**
	 * @return The entry point
	 */
	public static RefundTermUpdate getRefundTermUpdate() {
		if (refundTermUpdate == null) {
			refundTermUpdate = new RefundTermUpdate();
		}
		return refundTermUpdate;
	}
	
	/**
	 * Set the trade id and the refund id.
	 * 
	 * @param bizOrderId The trade id.
	 * @param refundId The refund id.
	 */
	public void setIds(String bizOrderId, String refundId) {
		input.setBiz_order_id(bizOrderId);
		input.setRefund_id(refundId);
	}
	
	@Override
	public void onClick(ClickEvent arg0) {
		// The buyer clicks the button to submit the request of modifying refund term
		if (arg0.getSource() == okButton) {
			
			if (input == null) {
				container.add(new Label("Login illegal, please login again"));
			} else {				
				input.setAuction_remark(aucRemarkTArea.getText().trim());
				input.setOrder_remark(ordRemarkTArea.getText().trim());
				
				RPC.getService().getUrl(input.getURLMap(), inputCallBack);
				
				if (f == null) {
					f = new Frame();
					container.add(f);
				}
			}
			
		}		
	}
	
	/**
	 * The private constructor
	 */
	private RefundTermUpdate() {
		String queryString = Window.Location.getQueryString();	
		
		System.out.println("Query string: " + (queryString == null ? "null" : queryString) );
		
		if (queryString != null && queryString.indexOf(RESULT_FLAG) >= 0) {
			// TODO Process the result
		} else {
			initialize();
		}
	}

	/**
	 * Initialize the GUI
	 */
	private void initialize() {		
		container.add(createHead("Modify Refund Term"));
		
		FlexTable flexTable = new FlexTable();
		flexTable.setWidget(0, 0, aucRemarkLabel);
		flexTable.setWidget(0, 1, aucRemarkTArea);
		aucRemarkTArea.setSize("400px", "100px");
		
		flexTable.setWidget(1, 0, ordRemarkLabel);
		flexTable.setWidget(1, 1, ordRemarkTArea);
		ordRemarkTArea.setSize("400px", "100px");
		
		flexTable.setWidget(2, 0, okButton);
		okButton.addClickHandler(this);

		flexTable.getCellFormatter().setHorizontalAlignment(2, 0, HorizontalPanel.ALIGN_CENTER);
		flexTable.getFlexCellFormatter().setColSpan(2, 0, 2);	
		DecoratorPanel dPanel = new DecoratorPanel();
		dPanel.setWidget(flexTable);
		container.add(dPanel);
			
		initWidget(container);
		
		// Initialize the input parameters
		input = new RefundHandleRequest();
		input.initialize();	
		input.setUser_action("1");
	}
	
	/**
	 * Create a head panel
	 * 
	 * @param title the text to be displayed in the head panel
	 * @return a panel that is displayed in the head part always
	 */
	private HeadPanel createHead(String title) {
		HeadPanel head = new HeadPanel();
		head.add(new Label(title));
		return head;
	}

}
